import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;

public class UdpServer {

    DatagramSocket socket = null; //先设置为空

    public UdpServer(int port) throws SocketException {

        socket = new DatagramSocket(port);  //告诉了socket中间人服务器的一个端口
    }

    public void start() throws IOException {
        System.out.println("服务端启动");  //服务器启动
        while (true) {
            DatagramPacket datagramPacket = new DatagramPacket(new byte[4096], 4096);

            socket.receive(datagramPacket);

            String request = new String(datagramPacket.getData(), 0, datagramPacket.getLength());//获取到了请求
            //计算请求
            String repose = getRepose(request);

            DatagramPacket datagramPacket1 = new DatagramPacket(repose.getBytes(), repose.getBytes().length, datagramPacket.getSocketAddress());
            socket.send(datagramPacket1);
            System.out.printf("[%s %d %s]\n", datagramPacket.getSocketAddress(), datagramPacket.getPort(), repose);
        }
    }

    public String getRepose(String s1) {
        return s1;

    }

    public static void main(String[] args) throws IOException {
        UdpServer udpServer = new UdpServer(9090);
        udpServer.start();
    }
}


